﻿using Csb.SignalServer.Hubs;
using Csb.SignalServer.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;

namespace Csb.SignalServer.Controllers
{
    
    [ApiController]
    [Route("[controller]")]
    public class HubController : ControllerBase
    {
        private readonly IHubContext<ChatHub> _hubContext;
        private static int count = 1;
        public HubController(IHubContext<ChatHub> hubContext)
        {
            _hubContext = hubContext;
        }

        /// <summary>
        /// 发布消息
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        [HttpGet("PushMessage")]
        public async Task PushMessage([FromQuery]string context)
        {
            //通过hub上下文获取客户端，并调用客户端中名为ShowMsg的方法，并传递了消息主题和内容
            await _hubContext.Clients.All.SendAsync("ShowMsg", new MsgInfo { message = context + "，这是第" + count++ + "次发布的消息\r\n" });
        }

    }
}
